package com.publicstaticdroid.g2l_interpreter_connect;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Interpreter Connect home screen.
 * @author dvhc, Nathan Breit
 *
 */
public class InterpreterHome extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		//This makes it so the default values in prefs.xml actually get used.
		PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
		
		setContentView(R.layout.home);
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		if(!settings.contains("phoneNumber")){
			//We need to make a popup to ask for the phonenumber
			showPhoneNumberDialog();
		}
		
		
	}
	@Override
	protected void onResume() {
		updateStats();
		super.onResume();
	}
	/**
	 * Update the stats displayed on the home screen.
	 */
	private void updateStats() {
		SharedPreferences settings = getSharedPreferences(Constants.PREFS_KEY, 0);
		TextView interpretationTimeView = (TextView) findViewById(R.id.interpretationTime);
		long timeInMillis = settings.getLong("interpretationTime", 0);
		interpretationTimeView.setText("Minutes spent interpreting: " + timeInMillis/(1000 * 60));
		TextView requestsAcceptedView = (TextView) findViewById(R.id.requestsAccepted);
		requestsAcceptedView.setText("Requests Accepted: " + settings.getInt("requestsAccepted", 0));
		TextView requestsRecievedView = (TextView) findViewById(R.id.requestsRecieved);
		requestsRecievedView.setText("Requests Recieved: " + settings.getInt("requestsRecieved", 0));
	}
	//Linked directly to button in main.xml
	public void startSettingsActivity(View view) {
		//Toast.makeText(getApplicationContext(), "This is only a demo", Toast.LENGTH_LONG).show();
		Intent prefsIntent = new Intent(getApplicationContext(), ApplicationPreferences.class);
		startActivity(prefsIntent);
		//SMSReceiver.makeCall(getApplicationContext(), "14252609126", "12345");
	}
	//Linked directly to button in main.xml
	public void launchScheduler(View view) {
		//Log.i("G2L",getMyPhoneNumber());
		//Toast.makeText(getApplicationContext(), "This is only a demo", Toast.LENGTH_LONG).show(); 
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		String phoneNumber = getMyPhoneNumber();
		if(phoneNumber.equals("")){
			phoneNumber = "unknown";
		}

		int versionCode = 0;
		try {
			versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String fullSchedulerURL = settings.getString("schedulerURL","no scheduler url")
				                  + "?phone_number=" + phoneNumber
				                  + "&version=" + versionCode;
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullSchedulerURL));
		startActivity(browserIntent);
	}
	private String getMyPhoneNumber()
	{
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		return settings.getString("phoneNumber", "unknown");
	}
	protected void showPhoneNumberDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("What is your phone number?");
		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String phoneNumber = tm.getLine1Number();

		input.setText(phoneNumber);
		
		builder.setView(input);
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString();
				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("phoneNumber", value);
				editor.commit();
				dialog.dismiss();
			}
		});
		builder.show();
	}
}
